<template>
	<view class="page">
		<map id="map" show-compass="true" show-location :latitude="latitude" :longitude="longitude"
			:markers="markers"></map>
		<view class="overlay">
			<button@click="position(22.836563,108.19688)">定位</button>
			<!-- <button @click="position(39.894589, 116.32125)">定位</button> -->
			<!-- <button @click="point">撒点</button>
			<button @click="line">划线</button>
			<button @click="area">绘制区域</button> -->

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				latitude: 39.901967, // 纬度
				longitude: 116.42932, // 经度
				markers: [], // 标记点数组
			};
		},
		methods: {
			//定位
					position(latitude, longitude) {
							wx.getLocation({
								type: 'gcj02',
								success: (res) => {
									this.latitude = latitude;
									this.longitude = longitude;
									this.addMarker(); // 添加标记
								},
								fail: (err) => {
									console.error('定位失败', err);
									uni.showToast({
										title: '定位失败，请检查权限',
										icon: 'none'
									});
								}
							});
						},
			getLocation() {
				// 定位逻辑
				wx.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.addMarker(); // 添加标记
					},
					fail: (err) => {
						console.error('定位失败', err);
						uni.showToast({
							title: '定位失败，请检查权限',
							icon: 'none'
						});
					}
				});
			},
			
			addMarker() {
				this.markers = [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					title: '我的位置',
				}];
			},
			
		},
		mounted() {
			// 页面加载时不自动定位，用户点击按钮后定位
		}
	};
</script>

<style>
	.container {
		position: relative;
		width: 100%;

	}

	#map {
		width: 100%;
		height: 100%;
	}

	.overlay {
		position: absolute;
		top: 10px;
		right: 10px;
		z-index: 10;
		/* 确保覆盖层在地图上方 */
	}

	button {
		background-color: #007aff;
		color: white;
		border: none;
		padding: 10px;
		border-radius: 5px;
	}
</style>